import xlrd
from xlrd import xldate_as_tuple
import datetime
import time
import os
import xml.dom.minidom as minidom
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ElementTree, Element
import numpy as np

"""
删除不含有目标框的xml文件
"""


xml_dir = '/home/xiang/cme/faster-rcnn-3quanlian/data/VOCdevkit2007/VOC2007/Annotations/'

def mod_xml():
    xmllist = list(os.listdir(xml_dir))
    tree = ElementTree()
    for xml in xmllist:
        xmlpath = os.path.join(xml_dir, xml)
        root = tree.parse(xmlpath)
        # size = root.find('size')
        # width = size.find('width')
        # if width.text == '360':
        #     width.text = '400'

        objects = root.findall('object')
        for object in objects:
            name = object.find('name')
            name.text ='cme'

        tree.write(xmlpath, encoding="utf-8", xml_declaration=True)

    #     if imgtoxml not in xmllist:
    #         delimgpath = os.path.join(img_dir, img)
    #         os.remove(delimgpath)
    #
    # tree = ElementTree()
    # if os.path.exists(out_path):
    #     root = tree.parse(out_path)
    # else:
    #     root = tree.parse(rootxml)
    # path = root.find('path')
    # path.text = imgpath
    # object = ET.SubElement(root, 'object')  # 生成GN对象节点
    # name = ET.SubElement(object, 'name')
    # name.text = 'cme'

if __name__ == '__main__':
    mod_xml()